package com.borazone.kilio.extra;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import com.jcraft.jzlib.ZInputStream;

public class ZLibExtractor {

	public static void main( String[] args ) {
		
		try {
			FileInputStream fin = new FileInputStream( args[0] );
			ZInputStream zin = new ZInputStream(fin);
			FileOutputStream fout = new FileOutputStream( args[0] + ".out" );
			
			byte[] buf = new byte[32];
			int len = 0;
			while( -1 < ( len = zin.read(buf) ) ) {
				fout.write( buf, 0, len );
			}
			
			fout.flush();
			fout.close();
			zin.close();
			fin.close();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public static void doSomething( String fileName ) {
		try {
			FileInputStream fin = new FileInputStream( fileName );
			
			ZInputStream zin = new ZInputStream(fin);
			FileOutputStream fout = new FileOutputStream( fileName );
			
			byte[] buf = new byte[32];
			int len = 0;
			while( -1 < ( len = zin.read(buf) ) ) {
				fout.write( buf, 0, len );
			}
			
			fout.flush();
			fout.close();
			zin.close();
			fin.close();
			
			File file2 = new File( fileName + ".out" );
			File file1 = new File( fileName );
			file1.delete();
			file2.renameTo( file1 );
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public static void doSomething2( String fileName ) {
		try {
			FileInputStream fin = new FileInputStream( fileName );
			
			fin.read();
			
			ZInputStream zin = new ZInputStream(fin);
			FileOutputStream fout = new FileOutputStream( fileName + ".out");
			
			byte[] buf = new byte[32];
			int len = 0;
			while( -1 < ( len = zin.read(buf) ) ) {
				fout.write( buf, 0, len );
			}
			
			fout.flush();
			fout.close();
			zin.close();
			fin.close();
			
			File file2 = new File( fileName + ".out" );
			File file1 = new File( fileName );
			file1.delete();
			file2.renameTo( file1 );
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}
